package com.sap.sap_mangers.service.Impl;

import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
import com.sap.sap_mangers.mapper.BillMapper;
import com.sap.sap_mangers.pojo.SapBill;
import com.sap.sap_mangers.pojo.SapBillImage;
import com.sap.sap_mangers.service.BillService;
import com.sap.sap_mangers.service.DynamicService;
import com.sap.sap_mangers.until.CosUntil;
import com.sap.sap_mangers.until.FileCheckUntil;
import com.sap.sap_mangers.until.SimpleJson;
import com.sap.sap_mangers.until.SnowflakeIdWorker;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author 阿东
 * @date 2022/8/29 [15:00]
 */
@Service
public class BillServiceImpl implements BillService {
    @Resource
    DataSourceTransactionManager transactionManager;
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private DynamicService dynamicService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SimpleJson addBill(Integer type, String context, BigDecimal price, Timestamp timestamp, String note, List<MultipartFile> images, String username, String name) throws Exception {
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0, 0);
        if (type == 0) {
            if (images == null || images.size() == 0) {
                return new SimpleJson(1, "上传支出账单必须附带证明材料(发票，账单之类)!");
            }
        }

        //可能出现异常的部分
        val groupImageId = snowflakeIdWorker.nextId();
        SapBill sapBill = new SapBill(type, context, price, username, name, new Timestamp(System.currentTimeMillis()), timestamp, note, groupImageId, Integer.valueOf(dynamicService.getYear()));
        billMapper.addSapBill(sapBill);
        if (images != null && images.size() != 0) {
            Boolean check = getUrl(images, groupImageId);
            if (!check) {
                //controller层捕获到此异常 要返回"图片上传错误"信息;如果不是此异常,说明是数据库操作异常，返回SimpleJson.FAILURE
                throw new IOException();
            }
        }
        return SimpleJson.SUCCESS;
    }


    private Boolean getUrl(List<MultipartFile> images, Long groupImageId) {
        for (MultipartFile image : images) {
            if (FileCheckUntil.checkImage(image)) {
                try {
                    CosUntil cosUntil = new CosUntil();
                    String cosUrl = cosUntil.getCosUrl(image, "SapBillImage/" + image.getOriginalFilename());
                    billMapper.addImage(new SapBillImage(cosUrl, groupImageId));
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public List<SapBill> getBill(Integer type, Integer year) {
        List<SapBill> bill = billMapper.getBill(type, year);
        for (SapBill sapBill : bill) {
            sapBill.setImagesList(billMapper.getImage(sapBill.getBillImage()));
        }
        return bill;
    }

    @Override
    public Integer getSumBill(Integer type, Integer year) {
        return billMapper.getSumBill(type, year);
    }
}
